package com.example.cinemasystemv2.service;

import com.example.cinemasystemv2.entity.ScreeningRoom;
import com.example.cinemasystemv2.entity.Screening;
import com.example.cinemasystemv2.repository.ScreeningRoomRepository;
import com.example.cinemasystemv2.repository.ScreeningRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ScreeningRoomService {
    
    @Autowired
    private ScreeningRoomRepository screeningRoomRepository;
    
    @Autowired
    private SystemLogService systemLogService;
    
    @Autowired
    private ScreeningRepository screeningRepository;
    
    public List<ScreeningRoom> getAllRooms() {
        return screeningRoomRepository.findAll();
    }
    
    public List<Screening> getScreeningsByRoom(ScreeningRoom room) {
        return screeningRepository.findByRoom(room);
    }
    
    public Optional<ScreeningRoom> getRoomById(Long id) {
        return screeningRoomRepository.findById(id);
    }
    
    public Optional<ScreeningRoom> getRoomByName(String name) {
        return screeningRoomRepository.findByName(name);
    }
    
    public List<ScreeningRoom> searchRooms(String keyword) {
        return screeningRoomRepository.searchRooms(keyword);
    }
    
    public ScreeningRoom saveRoom(ScreeningRoom room) {
        ScreeningRoom savedRoom = screeningRoomRepository.save(room);
        systemLogService.log("放映厅保存: " + room.getName(), "system", "SAVE_ROOM");
        return savedRoom;
    }
    
    public ScreeningRoom createRoom(String name, int rows, int seatsPerRow) {
        if (screeningRoomRepository.existsByName(name)) {
            throw new IllegalArgumentException("放映厅名称已存在");
        }
        
        ScreeningRoom room = new ScreeningRoom(name, rows, seatsPerRow);
        return saveRoom(room);
    }
    
    public ScreeningRoom updateRoom(ScreeningRoom room) {
        ScreeningRoom updatedRoom = screeningRoomRepository.save(room);
        systemLogService.log("放映厅更新: " + room.getName(), "system", "UPDATE_ROOM");
        return updatedRoom;
    }
    
    public void deleteRoom(Long id) {
        Optional<ScreeningRoom> roomOpt = screeningRoomRepository.findById(id);
        if (roomOpt.isPresent()) {
            ScreeningRoom room = roomOpt.get();
            
            // 检查是否有关联的场次
            List<Screening> screenings = screeningRepository.findByRoom(room);
            if (!screenings.isEmpty()) {
                throw new IllegalArgumentException("无法删除放映厅，该放映厅还有 " + screenings.size() + " 个场次安排");
            }
            
            screeningRoomRepository.deleteById(id);
            systemLogService.log("放映厅删除: " + room.getName(), "system", "DELETE_ROOM");
        }
    }
    
    public boolean existsByName(String name) {
        return screeningRoomRepository.existsByName(name);
    }
    
    public long getTotalRoomCount() {
        try {
            return screeningRoomRepository.countAllRooms();
        } catch (Exception e) {
            systemLogService.log("获取放映厅总数失败: " + e.getMessage(), "system", "ERROR");
            return 0; // 返回默认值
        }
    }
    
    public Long getTotalSeats() {
        try {
            return screeningRoomRepository.getTotalSeats();
        } catch (Exception e) {
            systemLogService.log("获取总座位数失败: " + e.getMessage(), "system", "ERROR");
            return 0L; // 返回默认值
        }
    }
    
    public void initializeDefaultRooms() {
        try {
            if (screeningRoomRepository.count() == 0) {
                createRoom("黄金厅", 8, 10);
                createRoom("白银厅", 10, 12);
                createRoom("翡翠厅", 7, 8);
                createRoom("钻石厅", 7, 8);
                systemLogService.log("初始化默认放映厅", "system", "INIT_ROOMS");
            }
        } catch (Exception e) {
            // 如果表还没有创建，先创建默认放映厅
            try {
                createRoom("黄金厅", 8, 10);
                createRoom("白银厅", 10, 12);
                createRoom("翡翠厅", 7, 8);
                createRoom("钻石厅", 7, 8);
                systemLogService.log("初始化默认放映厅", "system", "INIT_ROOMS");
            } catch (Exception ex) {
                systemLogService.log("初始化默认放映厅失败: " + ex.getMessage(), "system", "ERROR");
            }
        }
    }
}